package com.oa.modules.approvalsec1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oa.common.utils.AppUtils;
import com.oa.common.utils.Constant;
import com.oa.modules.approvalsec1.dao.ProcessDao;
import com.oa.modules.approvalsec1.dao.ProcessItemDao;
import com.oa.modules.approvalsec1.dao.ProjectScheduleDao;
import com.oa.modules.approvalsec1.entity.ProcessEntity;
import com.oa.modules.approvalsec1.entity.ProcessItemEntity;
import com.oa.modules.approvalsec1.entity.ProjectScheduleEntity;
import com.oa.modules.approvalsec1.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oa.common.utils.PageUtils;
import com.oa.common.utils.Query;


@Service("processService")
public class ProcessServiceImpl extends ServiceImpl<ProcessDao, ProcessEntity> implements ProcessService {
    @Autowired
    private ProcessItemDao processItemDao;
    @Autowired
    private ProcessDao processDao;
    @Autowired
    private ProjectScheduleDao projectScheduleDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        params.put("status", Constant.YES_OR_NO.YES.getValue());
        IPage page = AppUtils.getPage(new Query<ProcessEntity>(), params);
        return AppUtils.setPage(page, baseMapper.findPage(params), baseMapper.findRecords(page, params));
    }

    /**
     * 保存流程
     *
     * @param process
     */
    @Override
    public void saveProcess(ProcessEntity process, Integer[] itemlist) {
        baseMapper.add(process);
        Integer id = process.getId();
        this.saveProcessItem(id, itemlist);
    }

    /**
     * 根据流程id 修改节点
     *
     * @param id
     * @param itemList
     */
    @Override
    public void updateProcessItem(Integer id, Integer[] itemList) {
        this.saveProcessItem(id, itemList);
    }

    @Override
    public void setSort(Integer processId) {
        List<ProcessItemEntity> processList = processItemDao.selectList(new QueryWrapper<ProcessItemEntity>().eq("process_id", processId).eq("is_enabled", 1).groupBy("schedule_id"));
        int count = 1;
        for (int i = 0; i < processList.size(); i++) {
            processList.get(i).setSort(count);
            processItemDao.updateById(processList.get(i));
            count++;
        }
    }

    /**
     * 添加节点表数据
     */
    private void saveProcessItem(Integer processId, Integer[] itemList) {
        List<ProjectScheduleEntity> list = projectScheduleDao.selectList(new QueryWrapper<>());
        list.forEach(item -> {
            ProcessItemEntity processItemEntity = new ProcessItemEntity();
            processItemEntity.setScheduleId(item.getId());
            processItemEntity.setStatus(0);
            processItemEntity.setCreateDate(new Date());
            processItemEntity.setCreateOper("1");
            processItemEntity.setProcessId(processId);
            processItemEntity.setIsEnabled(0);
            processItemDao.insert(processItemEntity);
        });
        for (Integer scheduleId : itemList) {
            processItemDao.setIsEnabled(scheduleId, processId);
        }
        //排序，根据进度id进行排序
        this.setSort(processId);
    }


}
